package schoolword;

import java.util.Scanner;

public class Test2 {
    public static void main(String[] args) {
        Scanner sc = new  Scanner(System.in);
        System.out.println("请输入字符串: ");
        String str = sc.nextLine();
        System.out.println("请输入子串: ");
        String subStr = sc.nextLine();
        //求出现多少次
        int count = countString(str,subStr);
        System.out.println("\"" + subStr + "\"在\"" + str +"\"出现的次数是: " + count);
    }
    public static int countString(String str, String subStr) {
        int count = 0;
        int index = str.indexOf(subStr);  //找一下第一次出现的下标
        while (index != -1) {
            count++;
            index = str.indexOf(subStr,index+1);
        }
        return count;
    }
}
